/******************************
	loading data
******************************/

use PATH TO .DTA FILE in 2, clear // this and next line is only to get a list of variables to load -- this should speed up the real load two lines down
ds GMTdatetime zip, not
use `r(varlist)' using PATH TO .DTA FILE, clear
gen date=dofc(localdatetime)
gen byte hour=hh(localdatetime)
drop localdatetime


/******************************
	generating the various averages
******************************/
sort date location_code
tempfile NOAA_daily NOAA_6a4p NOAA_nightbefore
ds *_count // all variables counting the number of stations contributing to a particular station-averaged measurement should have the suffix _count
local counters `r(varlist)'
ds date hour location_code `counters', not
local weathervars `r(varlist)'

preserve
collapse (mean) `weathervars' (sum) `counters', by(date location_code) fast
rename (`weathervars' `counters') =_daily
compress
save ./NOAA_daily

restore, preserve
collapse (mean) `weathervars' (sum) `counters' if inrange(hour,6,15), by(date location_code) fast
rename (`weathervars' `counters') =_6a4p
save ./NOAA_6a4p

restore, preserve
drop if inrange(hour,6,19)
replace date = date+1 if inrange(hour,19,23)
collapse (mean) `weathervars' (sum) `counters', by(date location_code) fast
rename (`weathervars' `counters') =_nightbefore
compress
save ./NOAA_nightbefore

restore
egen byte third = cut(hour), icodes at(0(8)24)
replace third = third+1
collapse (mean) `weathervars' (sum) `counters', by(date location_code third) fast
compress
reshape wide `weathervars' `counters', i(date location_code) j(third)

foreach dataset in daily 6a4p nightbefore {
	merge 1:1 date location_code using ./NOAA_`dataset', nogenerate
	}
save ./NOAA_by_date.dta

